package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerMapper;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public IPage<Customer> PageInfoCustomer(Page<Customer> customerPage, String customerName) {

        if(customerName==null){
            customerName="";
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("customer_name",customerName);

        return customerMapper.selectPage(customerPage,queryWrapper);
    }

    @Override
    public ServiceVO saveCustmomer(Customer customer) {
        int flag = 0;
        if(customer.getCustomerId()==null){
            flag = customerMapper.insert(customer);
        }else {
            flag = customerMapper.updateById(customer);
        }
        ServiceVO serviceVO;
        if(flag!=0){
            serviceVO = new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
        }else
            serviceVO = new ServiceVO(ErrorCode.REQ_ERROR_CODE,ErrorCode.REQ_ERROR_MESS,null);
        return serviceVO;
    }

    @Override
    public ServiceVO deleteCustomer(String ids) {
        String[] split = ids.split(",");
        int flag = customerMapper.deleteBatchIds(Arrays.asList(split));
        ServiceVO serviceVO;
        if(flag!=0){
            serviceVO = new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
        }else
            serviceVO = new ServiceVO(ErrorCode.REQ_ERROR_CODE,ErrorCode.REQ_ERROR_MESS,null);
        return serviceVO;
    }

    @Override
    public List<Customer> ListCustomer(String customerName) {
        if(customerName==null){
            customerName="";
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("customer_name",customerName);
        List<Customer> list = customerMapper.selectList(queryWrapper);
        return list;
    }



}
